package com.ang.regularExpression;

/**
 * @author ssang
 * @date 2022-07-26 15:07
 */
public class PasswordValid {

    public static void main(String[] args) {
        valid("test1一?");
    }

    public static void valid(String password){
        //密码长度校验
        if (password.length() < Constants.LENGTH_MIN
                || password.length() > Constants.LENGTH_MAX)
        {
            System.out.println("密码长度在8-32位之间");
        }

        if(password.matches(Constants.REG_CHI)){
            System.out.println("密码不能包含汉字");
        }

        int matchNum = 0;
        if(password.matches(Constants.REG_NUM)){
            matchNum ++;
        }
        if(password.matches(Constants.REG_ENG)){
            matchNum ++;
        }
        if(password.matches(Constants.REG_CHAR)){
            matchNum ++;
        }

        //密码强度校验
        if(matchNum < Constants.PASSWORD_CHAR_NUM){
            System.out.println(String.format("密码必须包含2种以上字符（数字、字母和特殊字符：%s",Constants.SPECIAL_CHAR));
        }

        //非法特殊字符校验
        if(!password.matches(Constants.REG_ALL)){
            System.out.println(String.format("密码不能包含特殊字符，%s除外",Constants.SPECIAL_CHAR));
        }
    }

    /**
     * 内部类，定义密码规则
     */
    static class Constants{
        //密码长度匹配
        public static final int LENGTH_MIN = 8;
        public static final int LENGTH_MAX = 32;

        //密码包含的指定特殊字符
        public static final String SPECIAL_CHAR = "!@#$%_+=*";

        //正则
        public static final String REG_NUM = ".*[0-9].*";//包含数字
        public static final String REG_ENG = ".*[a-zA-z].*";//包含字母
        public static final String REG_CHAR = ".*["+SPECIAL_CHAR+"].*";//包含指定特殊符号
        public static final String REG_CHI = ".*[\\u4e00-\\u9fa5].*";//包含汉字
        public static final String REG_ALL = "^[A-Za-z0-9"+SPECIAL_CHAR+"]+$";//用来排除是否包含指定字符以外的特殊字符，比如：￥，()，~等
        public static final Integer PASSWORD_CHAR_NUM = 2;//密码强度，字符种类
    }
}
